<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="跌倒/坠床情况" >
            <div slot="left" style="display: flex; width: 100%; flex-wrap: wrap; ">
              <el-form-item slot="right" label="病⼈的意识状态" prop="consciousStatusArr" label-width="150px">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.consciousStatusArr">
                    <el-checkbox v-for="(item, index) in consciousStatusList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.consciousStatusArr && models.consciousStatusArr.indexOf('175') > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordAccidentFall.consciousStatusOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item slot="right" label="病⼈的活动能⼒" prop="actAbilityArr" label-width="150px">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.actAbilityArr">
                    <el-checkbox v-for="(item, index) in actAbilityList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.actAbilityArr && models.actAbilityArr.indexOf('183') > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordAccidentFall.actAbilityOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item slot="right" label="病⼈的⾃主照顾能⼒" prop="selfCareAbilityArr" label-width="150px">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.selfCareAbilityArr">
                    <el-checkbox v-for="(item, index) in selfCareAbilityList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.selfCareAbilityArr && models.selfCareAbilityArr.indexOf('193') > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordAccidentFall.selfCareAbilityOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="跌倒/坠床发⽣时患者处于的活动过程" prop="fallBedArr" label-width="150px">
                <div style="display: inline-block; width: 100%">
                  <el-checkbox-group v-model="models.fallBedArr" style="display: inline-block">
                    <el-checkbox
                      v-for="(item, index) in fallBedList"
                      :key="index"
                      :label="item.value"
                    >
                      <div style="display: flex;align-items: center;">
                        <div style="display: flex;align-items: center;">
                          {{ item.label }}
                        </div>
                        <!--        二维数据:床栏              -->
                        <div v-if="models.fallBedArr && models.fallBedArr.indexOf('200') > -1 && item.value === '200'" style="margin-left: 5px;display: flex;align-items: center;">
                          (
                          <el-checkbox-group v-model="models.fallBedHandrailArr">
                            <el-checkbox v-for="(item1, index) in fallBedHandrailList" :key="index" :label="item1.value"> {{ item1.label }} </el-checkbox>
                          </el-checkbox-group>
                          )
                        </div>
                        <!--        二维数据 : 陪护             -->
                        <div v-if="models.fallBedArr && models.fallBedArr.indexOf('201') > -1 && item.value === '201'" style="margin-left: 5px;display: flex;align-items: center;">
                          (
                          <el-checkbox-group v-model="models.fallBedAccompanyArr">
                            <el-checkbox v-for="(item2, index) in fallBedAccompanyList" :key="index" :label="item2.value"> {{ item2.label }} </el-checkbox>
                            <el-input
                              v-if="models.fallBedAccompanyArr && models.fallBedAccompanyArr.indexOf('214') > -1"
                              :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                              placeholder="请输入"
                              v-model="models.baqsjRecordAccidentFall.fallBedAccompanyOther"
                            />
                          </el-checkbox-group>
                          )
                        </div>
                      </div>
                    </el-checkbox>
                    <el-input
                      v-if="models.fallBedArr && models.fallBedArr.indexOf('205') > -1"
                      :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordAccidentFall.fallBedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
              <el-form-item slot="right" label="医疗介入" prop="medicalInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.medicalInvolvedArr">
                    <el-checkbox v-for="(item, index) in medicalInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.medicalInvolvedArr && models.medicalInvolvedArr.indexOf('125') > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.medicalInvolvedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div slot="right" style="width: 100%" />
              <el-form-item slot="right"  label="其他介入" prop="otherInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.otherInvolvedArr">
                    <el-checkbox v-for="(item, index) in otherInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.otherInvolvedArr && models.otherInvolvedArr.indexOf('134')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.otherInvolvedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </measure-two-info>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="跌倒/坠床情况">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">病⼈的意识状态</td>
                <td>{{ getConsciousStatus(models.consciousStatusArr) || '' }}</td>
                <td class="td-title">病⼈的活动能⼒</td>
                <td>{{ getActAbility(models.actAbilityArr) || '' }}</td>
                <td class="td-title">病⼈的⾃主照顾能⼒</td>
                <td>{{ getSelfCareAbility(models.selfCareAbilityArr) || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">跌倒/坠床发⽣时患者处于的活动过程</td>
                <td colspan="5">{{ getFallBedInfo(models.fallBedArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
            <tr slot="right">
              <td class="td-title">医疗介入</td>
              <td>{{ formatMedicalInterventions(models.medicalInvolvedArr) || '' }}</td>
            </tr>
            <tr slot="right">
              <td class="td-title">其他介入</td>
              <td>{{ formatOtherInterventions(models.otherInvolvedArr) || '' }}</td>
            </tr>
          </measure-two-info>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveAccidentFall } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureTwoInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureTwoInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'AccidentFall',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureTwoInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {},
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      consciousStatusList: [
        { value: '170', label: '清醒' },
        { value: '171', label: '嗜睡' },
        { value: '172', label: '意识混乱' },
        { value: '173', label: '昏迷' },
        { value: '174', label: '躁动' },
        { value: '175', label: '其他' }
      ],
      actAbilityList: [
        { value: '180', label: '使⽤辅助器具' },
        { value: '181', label: '残肢' },
        { value: '182', label: '⽆法⾛动' },
        { value: '183', label: '其他' }
      ],
      selfCareAbilityList: [
        { value: '190', label: '独⽴' },
        { value: '191', label: '需协助' },
        { value: '192', label: '完全依赖外界' },
        { value: '193', label: '其他' }
      ],
      fallBedList: [
        { value: '200', label: '床栏' },
        { value: '201', label: '陪护' },
        { value: '202', label: '使⽤约束带中' },
        { value: '203', label: '使⽤轮椅固定带中' },
        { value: '204', label: '⽆障碍物' },
        { value: '205', label: '其他' }
      ],
      fallBedHandrailList: [
        { value: '201', label: '未拉上' },
        { value: '202', label: '单侧或部分拉上' },
        { value: '203', label: '两侧全拉上' },
        { value: '204', label: '病床⽆床栏' },
        { value: '205', label: '不明' },
        { value: '206', label: '不适⽤' }
      ],
      fallBedAccompanyList: [
        { value: '211', label: '家属' },
        { value: '212', label: '看护' },
        { value: '213', label: '⼯作⼈员' },
        { value: '214', label: '其他' }
      ],
      medicalInterventionList: [
        { value: '120', label: '持续观察' },
        { value: '121', label: '加强护理防范' },
        { value: '122', label: '监测生命体征' },
        { value: '123', label: '伤口护理' },
        { value: '125', label: '其他' }
      ],
      otherInterventionList: [
        { value: '130', label: '予以病人家属慰问和支持' },
        { value: '131', label: '移除与危害相关的设备或环境运作' },
        { value: '134', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        consciousStatusArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.consciousStatus) ? info.baqsjRecordAccidentFall.consciousStatus.split(',') : [],
        actAbilityArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.actAbility) ? info.baqsjRecordAccidentFall.actAbility.split(',') : [],
        selfCareAbilityArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.selfCareAbility) ? info.baqsjRecordAccidentFall.selfCareAbility.split(',') : [],
        fallBedArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.fallBed) ? info.baqsjRecordAccidentFall.fallBed.split(',') : [],
        fallBedHandrailArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.fallBedHandrail) ? info.baqsjRecordAccidentFall.fallBedHandrail.split(',') : [],
        fallBedAccompanyArr: (info.baqsjRecordAccidentFall && info.baqsjRecordAccidentFall.fallBedAccompany) ? info.baqsjRecordAccidentFall.fallBedAccompany.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'AccidentFall',
        eventName: '意外事件-跌倒、坠床事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 事件差错详情 => 对象类型(不同的表单，定制不同)
        baqsjRecordAccidentFall: {},
        // 事件差错详情 => 意识状态 (不同的表单，定制不同)
        consciousStatusArr: [],
        // 事件差错详情 => 活动能力
        actAbilityArr: [],
        // 事件差错详情 => 自我照顾能力
        selfCareAbilityArr: [],
        // 事件差错详情 => 跌倒/坠床发⽣时患者处于的活动过程
        fallBedArr: [],
        // 事件差错详情 => 活动过程:床栏状态(1)
        fallBedHandrailArr: [],
        // 事件差错详情 => 活动过程: 陪护(2)
        fallBedAccompanyArr: []
      }
      this.dialogVisible = true
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',');
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',');
          this.models.baqsjRecordAccidentFall.consciousStatus = this.models.consciousStatusArr.join(',');
          this.models.baqsjRecordAccidentFall.actAbility = this.models.actAbilityArr.join(',');
          this.models.baqsjRecordAccidentFall.selfCareAbility = this.models.selfCareAbilityArr.join(',');
          this.models.baqsjRecordAccidentFall.fallBed = this.models.fallBedArr.join(',');
          this.models.baqsjRecordAccidentFall.fallBedHandrail = this.models.fallBedHandrailArr.join(',');
          this.models.baqsjRecordAccidentFall.fallBedAccompany = this.models.fallBedAccompanyArr.join(',');

          doSaveAccidentFall(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    },
    getConsciousStatus(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.consciousStatusList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '175') {
            labels.push(`其他（${this.models.baqsjRecordAccidentFall.consciousStatusOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getActAbility(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.actAbilityList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '183') {
            labels.push(`其他（${this.models.baqsjRecordAccidentFall.actAbilityOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getSelfCareAbility(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.selfCareAbilityList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '193') {
            labels.push(`其他（${this.models.baqsjRecordAccidentFall.selfCareAbilityOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getFallBedInfo(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.fallBedList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '200') {
            const handrailLabels = [];
            this.fallBedHandrailList.forEach(handrailItem => {
              if (this.models.fallBedHandrailArr && this.models.fallBedHandrailArr.includes(handrailItem.value)) {
                handrailLabels.push(handrailItem.label);
              }
            });
            labels.push(`${item.label}（${handrailLabels.join(', ')}）`);
          } else if (item.value === '201') {
            const accompanyLabels = [];
            this.fallBedAccompanyList.forEach(accompanyItem => {
              if (this.models.fallBedAccompanyArr && this.models.fallBedAccompanyArr.includes(accompanyItem.value)) {
                if (accompanyItem.value === '214') {
                  accompanyLabels.push(`其他（${this.models.baqsjRecordAccidentFall.fallBedAccompanyOther || ''}）`);
                } else {
                  accompanyLabels.push(accompanyItem.label);
                }
              }
            });
            labels.push(`${item.label}（${accompanyLabels.join(', ')}）`);
          } else if (item.value === '205') {
            labels.push(`其他（${this.models.baqsjRecordAccidentFall.fallBedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    formatMedicalInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.medicalInterventionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '125') {
            labels.push(`其他（${this.models.baqsjRecordHandle.medicalInvolvedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    formatOtherInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.otherInterventionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '134') {
            labels.push(`其他（${this.models.baqsjRecordHandle.otherInvolvedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
